package com.cneport.transfer.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;

public abstract class DateUtil {
    public static final XMLGregorianCalendar convert2XMLGregorianCalendar(
            Date date) {
        XMLGregorianCalendar xmlGregorianCalendar = null;
        if (date != null) {
            GregorianCalendar gregorianCalendar = new GregorianCalendar();
            gregorianCalendar.setTime(date);
            xmlGregorianCalendar = new XMLGregorianCalendarImpl(
                    gregorianCalendar);
        }
        return xmlGregorianCalendar;
    }
    /**
     * XMLGregorianCalendar日期转换为java.util.Date
     * @param xmlGregorianCalendar
     * @return
     */
    public static final Date convert2Date(
            XMLGregorianCalendar xmlGregorianCalendar) {
        Date date = null;
        if (xmlGregorianCalendar != null) {
            date = xmlGregorianCalendar.toGregorianCalendar().getTime();
        }
        return date;
    }
    
    public static final String convertDateToYYYYMMdd(Date aDate) {
        return getDateTime("yyyyMMdd", aDate);
    }
    
    
    public static final String convertDateToYYYYMMddHHmmSS(Date aDate) {
        return getDateTime("yyyyMMddHHmmss", aDate);
    }
    
    public static final String convertDateToYYYYMMddHHmmzzz(Date aDate) {
        if(aDate==null){
            return null;
        }
        return getDateTime("yyyyMMddHHmm", aDate)+"+08";
    }
    public static final String convertDateToYYYYMMddHHmmsszzz(Date aDate) {
        if(aDate==null){
            return null;
        }
        return getDateTime("yyyyMMddHHmmss", aDate)+"+08";
    }
    //add by sxy
    public static final String convertDateToHHmmsszz(Date aDate) {
        if(aDate==null){
            return null;
        }
        String timeTemp = getDateTime("yyyyMMddHHmmss", aDate)+"00";
        String time = timeTemp.substring(8);
        return time;
    }
    
    public static final String convertDateToYYYYMMddHHmmssSSS(Date aDate) {
        if(aDate==null){
            return null;
        }
        return getDateTime("yyyyMMddHHmmssSSS", aDate);
    }
    public static final String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null && aMask!=null && !aMask.equals("")) {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }
    public static Date addDataDaysCleanLow(Date dt, int n) {
        try {
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt);
            rightNow.set(Calendar.HOUR, 0);
            rightNow.set(Calendar.MINUTE, 0);
            rightNow.set(Calendar.SECOND, 0);
            rightNow.add(Calendar.DATE, n);
            Date dt1 = rightNow.getTime();
            return dt1;
        } catch (Exception ex) {
            return null;
        }
    }
}
